package com.chess.chessplay;


import com.chess.chessplay.pojo.Eval;
import org.junit.jupiter.api.Test;

import java.io.*;

public class test {

    public String convertToString(String[][] array, int row, int col) {

        String str = "";
        String tempStr = null;
        str+='"';
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                tempStr = array[i][j];
                str = str + tempStr + ",";
            }
        }
        str+='"';
        return str;
    }

    public String[][]	convertToArray(String str, int row, int col){
        String[][]	arrayConvert = new String[row][col];
        int count = 0;
        String[] strArray = str.split(",");
        for(int i = 0 ; i < row ; i ++){
            for(int j = 0 ; j < col ; j ++){
                arrayConvert[i][j] = strArray[count];
                ++ count ;
            }
        }
        return arrayConvert;
    }

    @Test
    public void test(){
        String[][] p = {{"5","7"}, {"6","8"}};
        String pchess = "String[][] l = new String[2][2];" +
                "        for(int i = 0; i < m.length; i++){" +
                "            for(int j = 0; j < m[i].length; j++){" +
                "                l[j][i] = m[i][j];" +
                "            }" +
                "        }" +
                "        return l;";
        String s = convertToString(p, 2, 2);
        String code =
                "public class test {\n" +
                        "\n" +
                        "    public static void main(String[] args) {\n" +
                        "        String[][] a = new String[2][2];\n" +
                        "        String s = "+ s +";"+
                        "        String[][] b = convertToArray(s, 2, 2);\n" +
                        "        a = x(b);\n" +
                        "        for(int i = 0; i < a.length; i++){\n" +
                        "            for(int j = 0; j < a[i].length; j++){\n" +
                        "                System.out.println(a[i][j]);\n" +
                        "            }\n" +
                        "        }\n" +
                        "    }\n" +
                        "\n" +
                        "    public int getRow(String[][] array) {\n" +
                        "        int row = 0;\n" +
                        "        if (array != null) {\n" +
                        "            row = array.length; \n" +
                        "        }\n" +
                        "        return row;\n" +
                        "    }\n" +
                        "\n" +
                        "    public int getCol(String[][] array) {\n" +
                        "        int col = 0;\n" +
                        "        if (array != null) {\n" +
                        "            col = array[0].length; \n" +
                        "        }\n" +
                        "        return col;\n" +
                        "    }\n" +
                        "\n" +
                        "    public static String convertToString(String[][] array, int row, int col) {\n" +
                        "        String str = \"\";\n" +
                        "        String tempStr = null;\n" +
                        "        for (int i = 0; i < row; i++) {\n" +
                        "            for (int j = 0; j < col; j++) {\n" +
                        "                tempStr = array[i][j];\n" +
                        "                str = str + tempStr + \",\";\n" +
                        "            }\n" +
                        "        }\n" +
                        "        return str;\n" +
                        "    }\n" +
                        "\n" +
                        "    public static String[][]\tconvertToArray(String str, int row, int col){\n" +
                        "        String[][]\tarrayConvert = new String[row][col];\n" +
                        "        int count = 0;\n" +
                        "        String[] strArray = str.split(\",\");\n" +
                        "        for(int i = 0 ; i < row ; i ++){\n" +
                        "            for(int j = 0 ; j < col ; j ++){\n" +
                        "                arrayConvert[i][j] = strArray[count];\n" +
                        "                ++ count ;\n" +
                        "            }\n" +
                        "        }\n" +
                        "        return arrayConvert;\n" +
                        "    }\n" +
                        "\n" +
                        "    public static String[][] x(String[][] m){\n" +
                        pchess+
                        "    }\n" +
                        "}";
        Eval eval = new Eval(code);
        eval.compiler();
        eval.run();
    }

    public static void writeFileContent(String fileName, String content){

        try {
            File file = new File(fileName);
            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            System.out.println("Done");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

    @Test
    public void test2(){
        //总的框架
        //java类和类文件的名字要一样，所以修改AdminCode.txt的内容时，要把file_name对应的.java文件名给改了
        String admin_file_path = "E:\\java\\Test";
        String admin_file_name = "AdminCode.txt";
        String adcode = readFileContent(admin_file_path+"\\"+admin_file_name);

        //用户输入代码
//        String user_file_path = "E:\\java\\Test";
//        String user_file_name = "MyCode.txt";
//        String mycode = readFileContent(user_file_path+"\\"+user_file_name);

        //输出答案的文件
        String ans_file_path = "E:\\java\\Test";
        String ans_file_name = "answer.txt";


        String mycode = "//123";
        int size = adcode.length()-1;
        while(adcode.charAt(size) != '}'){
            size--;
        }
        adcode = adcode.substring(0, size);
        adcode += mycode;
        adcode += "\n}";
//        Eval eval = new Eval(adcode);
        writeFileContent("E:\\java\\Test\\computer.java", adcode);
        Eval eval = new Eval("E:\\java\\Test\\","computer.java");
        eval.compiler();
        eval.run();

//        String answer = readFileContent(ans_file_path+"\\"+ans_file_name);
//        System.out.println(answer);

    }
}

